home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 09 - QuickDraw 3D / Rotation / Mesh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-21  |  7.8 KB  |  309 lines  |  [TEXT/MPCC]

  1. /****************************/
  2. /*               MESH.C            */
  3. /****************************/
  4.  
  5.  
  6. /****************************/
  7. /*    EXTERNALS             */
  8. /****************************/
  9.  
  10. #include <qd3d.h>
  11. #include <QD3DGeometry.h>
  12. #include <QD3DSet.h>
  13. #include <QD3DView.h>
  14. #include <QD3DShader.h>
  15. #include <QD3DGroup.h>
  16. #include <QD3DRenderer.h>
  17. #include <QD3DAcceleration.h>
  18. #include <QD3DDrawContext.h>
  19. #include <QD3DTransform.h>
  20.  
  21. extern    TQ3ViewObject        gMyViewObject;
  22. extern    TQ3StyleObject        gMyStyleObject_Interpolation;
  23. extern    TQ3StyleObject        gMyStyleObject_Backfacing;
  24. extern    TQ3StyleObject        gMyStyleObject_Fillstyle;
  25. extern    TQ3ShaderObject        gMyShaderObject_IlluminationShader;
  26.  
  27.  
  28.  
  29. /****************************/
  30. /*    PROTOTYPES            */
  31. /****************************/
  32.  
  33. extern    void DoFatalAlert(Str255 s);
  34.  
  35. void DrawMyObjects(void);
  36. void CreateMyMesh(void);
  37. TQ3GroupObject MakeMyRotationGroup(float x, float y, float z);
  38.  
  39.  
  40. /****************************/
  41. /*    CONSTANTS             */
  42. /****************************/
  43.  
  44.  
  45. /*********************/
  46. /*    VARIABLES      */
  47. /*********************/
  48.  
  49. TQ3GroupObject        gMyMeshGroup;
  50.  
  51.  
  52. /****************** CREATE MY MESH ******************/
  53. //
  54. // Creates a mesh geometry object.
  55. //
  56. // INPUT    : none
  57. // OUTPUT    : none
  58. //
  59.  
  60. void CreateMyMesh(void)
  61. {
  62. long                i,faceNum,vertexNum;
  63. float                ambient = 1.0;
  64. TQ3ColorRGB            color;
  65. TQ3AttributeSet        vertexAttribs;
  66. TQ3Vertex3D            vertex;
  67. TQ3MeshFace            face;
  68. TQ3MeshVertex        meshVertexIDList[16];
  69. TQ3MeshVertex        faceVerts[4];
  70. TQ3GeometryObject    meshObj;
  71. TQ3GroupObject        groupObject;
  72. TQ3GroupPosition    myGroupPosition;
  73.  
  74. TQ3Point3D            meshVertexCoords[16] =
  75.                     {
  76.                         -10,0,-10,    -5,0,-10,    5,0,-10,    10,0,-10,
  77.                         -10,0,-5,    -5,0,-5,    5,0,-5,        10,0,-5,
  78.                         -10,0,5,    -5,0,5,        5,0,5,        10,0,5,
  79.                         -10,0,10,    -5,0,10,    5,0,10,        10,0,10
  80.                     };
  81.  
  82. long                meshFaceVertices[9*4] =
  83.                     {
  84.                         0,4,5,1,    1,5,6,2,    2,6,7,3,
  85.                         4,8,9,5,    5,9,10,6,    6,10,11,7,
  86.                         8,12,13,9,    9,13,14,10,    10,14,15,11
  87.                     };
  88.  
  89.  
  90.             /* CREATE NEW MESH OBJECT */
  91.             
  92.     meshObj = Q3Mesh_New();
  93.     if (meshObj == nil)
  94.         DoFatalAlert("\pError making new mesh!");
  95.         
  96.  
  97.             /***********************************/
  98.             /* CREATE THE VERTICES OF THE MESH */
  99.             /***********************************/
  100.  
  101.     for (vertexNum = 0; vertexNum < 16; vertexNum++)
  102.     {
  103.             /* MAKE ATTRIBUTES FOR THIS VERTEX */
  104.  
  105.         vertexAttribs = Q3AttributeSet_New();    // make attribs
  106.         if (vertexAttribs == nil)
  107.             DoFatalAlert("\pError making new vertex attribs.");
  108.         
  109.         color.r = (float)1/((Random()&0xf)+1);    // make random RGB color
  110.         color.g = (float)1/((Random()&0xf)+1);    // for each vertex
  111.         color.b = (float)1/((Random()&0xf)+1);
  112.  
  113.         Q3AttributeSet_Add(vertexAttribs,         // set color
  114.                 kQ3AttributeTypeDiffuseColor, &color);
  115.                 
  116.         Q3AttributeSet_Add(vertexAttribs,         // set ambient
  117.                 kQ3AttributeTypeAmbientCoefficient, &ambient);
  118.  
  119.  
  120.                 /* SET COORDS & ATTRIBS FOR VERTEX */
  121.  
  122.         vertex.point.x = meshVertexCoords[vertexNum].x;    // copy x,y,z coords
  123.         vertex.point.y = meshVertexCoords[vertexNum].y;
  124.         vertex.point.z = meshVertexCoords[vertexNum].z;
  125.         vertex.attributeSet = vertexAttribs;            // attach attribs
  126.  
  127.  
  128.             /* CREATE A NEW VERTEX & KEEP THE RETURNED VERTEX ID */
  129.  
  130.         meshVertexIDList[vertexNum]  = Q3Mesh_VertexNew(meshObj, &vertex);
  131.         
  132.  
  133.             /* DISPOSE OF OUR REFERENCE TO THE ATTRIBUTE SET */
  134.  
  135.         Q3Object_Dispose(vertexAttribs);
  136.     }        
  137.  
  138.             /*****************************/
  139.             /* CREATE MESH POLYGON FACES */
  140.             /*****************************/
  141.                         
  142.     for (i = 0, faceNum=0; faceNum < 9; faceNum++)
  143.     {
  144.                 /* GET VERTEX ID’S FROM TABLE */
  145.  
  146.         faceVerts[0] = meshVertexIDList[meshFaceVertices[i++]];    // copy vertex
  147.         faceVerts[1] = meshVertexIDList[meshFaceVertices[i++]];    // ID’S into local
  148.         faceVerts[2] = meshVertexIDList[meshFaceVertices[i++]];    // array for easy
  149.         faceVerts[3] = meshVertexIDList[meshFaceVertices[i++]];    // use.
  150.  
  151.  
  152.                 /* MAKE FACE WITH NO ATTRIBUTES */
  153.  
  154.         face = Q3Mesh_FaceNew(meshObj,4,&faceVerts[0],nil);        // make the face
  155.         if (face == nil)
  156.             DoFatalAlert("\pError adding face to mesh object!");
  157.     }
  158.     
  159.                 /*********************/
  160.                 /*    DO THE ROTATION  */
  161.                 /*********************/
  162.             
  163.             
  164.         /* CREATE A GROUP WITH ROTATION TRANSFORMS */
  165.  
  166.     groupObject = MakeMyRotationGroup(0.1,0.1,-0.4);        
  167.     
  168.  
  169.                 /* ADD MESH TO THE GROUP */
  170.  
  171.     myGroupPosition = Q3Group_AddObject(groupObject, meshObj);
  172.     if ( myGroupPosition == nil )
  173.         DoFatalAlert("\pQ3Group_AddObject failed!");    
  174.  
  175.  
  176.             /* DISPOSE OF OUR REFERENCE TO THE MESH OBJECT */
  177.     
  178.     Q3Object_Dispose(meshObj);
  179.  
  180.     gMyMeshGroup = groupObject;
  181. }
  182.  
  183.  
  184. /***************** MAKE MY ROTATION GROUP ********************/
  185. //
  186. // Creates a rotate transform object for all three axes, then combines them
  187. // into a single rotation group object which is returned to the caller.
  188. //
  189. // INPUT: x,y,z = radian degrees to rotate on each axis
  190. //
  191.  
  192. TQ3GroupObject MakeMyRotationGroup(float x, float y, float z)
  193. {
  194. TQ3GroupObject            rotGroup;
  195. TQ3RotateTransformData    xformData;
  196. TQ3TransformObject        rotObject;
  197. TQ3GroupPosition        myGroupPosition;
  198.  
  199.                     /* CREATE A GROUP */
  200.     
  201.     rotGroup= Q3OrderedDisplayGroup_New();
  202.     if ( rotGroup== nil )
  203.         DoFatalAlert("\pCouldnt make rotation group!");
  204.  
  205.  
  206.             /* CREATE AN X-AXIS ROTATE TRANSFORMATION */
  207.                 
  208.     xformData.axis = kQ3AxisX;
  209.     xformData.radians = x;
  210.     rotObject = Q3RotateTransform_New(&xformData);    // create new xform object
  211.  
  212.     myGroupPosition = Q3Group_AddObject(rotGroup, rotObject);    // add to group
  213.     if ( myGroupPosition == 0 )
  214.         DoFatalAlert("\pError adding x-axis rotation object to rot group!");
  215.     Q3Object_Dispose(rotObject);                            // dispose our reference
  216.  
  217.  
  218.                 /* CREATE A Y-AXIS ROTATE XFORM */
  219.                 
  220.     xformData.axis = kQ3AxisY;
  221.     xformData.radians = y;
  222.     rotObject = Q3RotateTransform_New(&xformData);    // create new xform object
  223.  
  224.     myGroupPosition = Q3Group_AddObject(rotGroup, rotObject);    // add to group
  225.     if ( myGroupPosition == 0 )
  226.         DoFatalAlert("\pError adding y-axis rotation object to rot group!");
  227.     Q3Object_Dispose(rotObject);                            // dispose our reference
  228.  
  229.  
  230.                 /* CREATE A Z-AXIS ROTATE XFORM */
  231.  
  232.     xformData.axis = kQ3AxisZ;
  233.     xformData.radians = z;
  234.     rotObject = Q3RotateTransform_New(&xformData);    // create new xform object
  235.  
  236.     myGroupPosition = Q3Group_AddObject(rotGroup, rotObject);    // add to group
  237.     if ( myGroupPosition == 0 )
  238.         DoFatalAlert("\pError adding z-axis rotation object to rot group!");
  239.     Q3Object_Dispose(rotObject);                            // dispose our reference
  240.  
  241.  
  242.                         /* RETURN THE GROUP */
  243.  
  244.     return(rotGroup);                                                
  245. }
  246.  
  247.  
  248. /******************* DRAW MY OBJECTS *********************/
  249. //
  250. // Draws a frame of animation for the FlyThru window.
  251. //
  252.  
  253. void DrawMyObjects(void)
  254. {
  255. TQ3Status                myStatus;
  256. TQ3ViewStatus            myViewStatus;
  257.  
  258.                 /* START RENDERING */
  259.                                 
  260.     myStatus = Q3View_StartRendering(gMyViewObject);            // start rendering
  261.     if ( myStatus == kQ3Failure )
  262.         DoFatalAlert("\p Q3View_StartRendering Failed!");
  263.  
  264.     do
  265.     {
  266.                 /* SET INTERPOLATION STYLE */
  267.  
  268.         myStatus = Q3Style_Submit(gMyStyleObject_Interpolation,gMyViewObject);
  269.         if ( myStatus == kQ3Failure )
  270.             DoFatalAlert("\p Q3Style_Submit Failed!");
  271.             
  272.                 /* SET BACKFACING STYLE */
  273.                     
  274.         myStatus = Q3Style_Submit(gMyStyleObject_Backfacing,gMyViewObject);
  275.         if ( myStatus == kQ3Failure )
  276.             DoFatalAlert("\p Q3Style_Submit Failed!");
  277.             
  278.             
  279.                     /* SET FILL STYLE */
  280.                     
  281.         myStatus = Q3Style_Submit(gMyStyleObject_Fillstyle, gMyViewObject);
  282.         if ( myStatus == kQ3Failure )
  283.             DoFatalAlert("\p Q3Style_Submit Failed!");
  284.             
  285.             
  286.                     /* SET SHADER TO USE */
  287.                     
  288.         myStatus = Q3Shader_Submit(gMyShaderObject_IlluminationShader, gMyViewObject);
  289.         if ( myStatus == kQ3Failure )
  290.             DoFatalAlert("\p Q3Shader_Submit Failed!");
  291.             
  292.             
  293.                 /* DRAW THE MESH GROUP OBJECT */
  294.                 
  295.         myStatus = Q3DisplayGroup_Submit(gMyMeshGroup,gMyViewObject);
  296.         if ( myStatus == kQ3Failure )
  297.             DoFatalAlert("\p Drawing Mesh Failed!");
  298.         
  299.         
  300.                     /* SEE IF DONE */
  301.                     
  302.         myViewStatus = Q3View_EndRendering(gMyViewObject);
  303.         
  304.     } while ( myViewStatus == kQ3ViewStatusRetraverse );
  305. }
  306.  
  307.  
  308.  
  309.